package com.cheer.ba.choosepic.ui;

import android.content.Intent;
import android.text.TextUtils;
import android.view.LayoutInflater;
import android.view.View;
import android.widget.AdapterView;
import android.widget.Button;
import android.widget.CheckBox;
import android.widget.GridView;
import android.widget.ImageView;
import android.widget.TextView;
import android.widget.Toast;
import android.widget.ToggleButton;

import com.bigkoo.svprogresshud.SVProgressHUD;
import com.cheer.ba.R;
import com.cheer.ba.choosepic.adp.AlbumGridViewAdp;
import com.cheer.ba.choosepic.util.AlbumHelper;
import com.cheer.ba.choosepic.util.Bimp;
import com.cheer.ba.choosepic.util.BitmapCache;
import com.cheer.ba.choosepic.util.ImageBucket;
import com.cheer.ba.choosepic.util.ImageItem;
import com.cheer.ba.choosepic.util.PublicWay;
import com.cheer.ba.choosepic.util.Res;
import com.cheer.ba.constant.IntentKey;
import com.cheer.ba.event.BaseEvent;
import com.cheer.ba.utils.LogUtils;
import com.cheer.ba.utils.StringUtils;
import com.cheer.ba.utils.ToastUtil;
import com.cheer.ba.view.base.BaseCommAty;
import com.cheer.ba.view.base.CommonAdapter;
import com.cheer.ba.view.base.ViewHolder;
import com.cheer.ba.widget.MyGifView;

import org.greenrobot.eventbus.EventBus;
import org.greenrobot.eventbus.Subscribe;
import org.greenrobot.eventbus.ThreadMode;

import java.util.ArrayList;
import java.util.List;

import butterknife.BindView;
import butterknife.OnClick;

/**
 * 描述 ：相册
 * 作者 ：hq
 * 时间 ：2017/7/30 15:36
 */
public class AlbumAty extends BaseCommAty implements AdapterView.OnItemClickListener {

    //标题
    @BindView(R.id.id_album_title_txt)
    TextView mTxtTitle;

    //图片选中数量
    @BindView(R.id.id_pic_num_txt)
    TextView mTxtChooseNum;

    //没有图片
    @BindView(R.id.id_no_pic_txt)
    TextView mTxtNoPic;

    @BindView(R.id.id_pic_grid)
    GridView mGridView;
    private AlbumGridViewAdp mAlbumAdp;
    //存储相片数据
    public static ArrayList<ImageItem> dataList;
    //存储所有文件夹里相片数据
    public static List<ImageBucket> contentList;

    //相册工具类
    private AlbumHelper helper;

    @Override
    protected boolean isTitleBarShow() {
        return false;
    }

    @Override
    public View loadView(LayoutInflater inflater, View childView) {
        return super.loadView(inflater, inflater.inflate(R.layout.aty_album, null, false));
    }

    @Override
    public void initView() {
        helper = AlbumHelper.getHelper();
        helper.init(getApplicationContext());

        contentList = helper.getImagesBucketList(false);
        dataList = new ArrayList<ImageItem>();
        Bimp.allPics.clear();
        for (int i = 0; i < contentList.size(); i++) {
            List<ImageItem> mImgs = contentList.get(i).imageList;
            dataList.addAll(mImgs);
            Bimp.allPics.addAll(mImgs);
        }
        hadPic();
        handPicNum();


        EventBus.getDefault().register(this);
    }

    @Override
    public void initData() {
        mAlbumAdp = new AlbumGridViewAdp(this, dataList, Bimp.tempSelectBitmap);
        mGridView.setAdapter(mAlbumAdp);
        mGridView.setOnItemClickListener(this);
        mAlbumAdp.setOnItemClickListener(new AlbumGridViewAdp.OnItemClickListener() {
            @Override
            public void onItemClick(CheckBox toggleButton, int position, boolean isChecked) {
                if (Bimp.tempSelectBitmap.size() >= PublicWay.total_num) {
                    toggleButton.setChecked(false);
                    toggleButton.setBackgroundResource(R.drawable.ic_agree_oil_ask_off);
                    if (!removeOneData(dataList.get(position))) {
                        ToastUtil.showToast("最多只能选择9张照片");
                    }
                    return;
                }
                if (isChecked) {
                    toggleButton.setBackgroundResource(R.drawable.ic_agree_oil_ask);
                    Bimp.tempSelectBitmap.add(dataList.get(position));
                } else {
                    toggleButton.setBackgroundResource(R.drawable.ic_agree_oil_ask_off);
                    Bimp.tempSelectBitmap.remove(dataList.get(position));
                }

                handPicNum();

                BaseEvent event = new BaseEvent();
                event.type = IntentKey.General.KEY_FINISH;
                EventBus.getDefault().post(event);
            }
        });
    }

    private boolean removeOneData(ImageItem imageItem) {
        if (Bimp.tempSelectBitmap.contains(imageItem)) {
            Bimp.tempSelectBitmap.remove(imageItem);
            return true;
        }
        return false;
    }

    @Override
    public void onItemClick(AdapterView<?> parent, View view, int position, long id) {
        Intent look = new Intent(aty, GalleryChooseAty.class);
        look.putExtra(IntentKey.General.KEY_POS, position);
        startActivity(look);
    }

    public void hadPic() {
        if (dataList.isEmpty()) {
            mTxtNoPic.setVisibility(View.VISIBLE);
            mGridView.setVisibility(View.GONE);
        } else {
            mTxtNoPic.setVisibility(View.GONE);
            mGridView.setVisibility(View.VISIBLE);
        }
    }

    /**
     * 处理下方图片定位
     */
    public void handPicNum() {
        mTxtChooseNum.setText(Bimp.tempSelectBitmap.size() + "/" + PublicWay.total_num);
    }


    @OnClick({R.id.id_pic_back_txt, R.id.id_jump_album_txt, R.id.id_look_all_pic_txt})
    public void onClick(View mView) {
        switch (mView.getId()) {
            case R.id.id_pic_back_txt:
                //返回
                finish();
                break;
            case R.id.id_jump_album_txt:
                //进入相册
                Intent intent = new Intent(aty, ImageFileAty.class);
                startActivity(intent);
                break;
            case R.id.id_look_all_pic_txt:
                //预览全部
                if (Bimp.tempSelectBitmap.size() > 0) {
                    Intent look = new Intent(aty, GalleryAty.class);
                    look.putExtra(IntentKey.General.KEY_POS, 0);
                    look.putExtra(IntentKey.General.KEY_BOOLEAN, true);
                    startActivity(look);
                } else {
                    SVProgressHUD.showInfoWithStatus(aty, "请至少选择一张图片");
                }
                break;
        }
    }


    @Subscribe(threadMode = ThreadMode.MAIN)
    public void eventUpdate(BaseEvent event) {
        //返回类型
        String type = event.type;
        if (TextUtils.equals(type, IntentKey.General.KEY_FINISH)) {
            //图片删除,处理数据
            mAlbumAdp.notifyDataSetChanged();

            handPicNum();
        } else if (TextUtils.equals(type, IntentKey.General.KEY_REFRESH_DATA)) {
            //列表数据刷新
            int position = event.pos;
            String folderName = AlbumAty.contentList.get(position).bucketName;
            mTxtTitle.setText(folderName);

            dataList.clear();
            dataList.addAll(contentList.get(position).imageList);

            Bimp.allPics.clear();
            Bimp.allPics.addAll(dataList);

            mAlbumAdp.notifyDataSetChanged();
        }
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        EventBus.getDefault().unregister(this);
    }
}
